﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

namespace ns
{
    public abstract class FSMState : MonoBehaviour
    {
        public FSMStateID StateID { get; set; }
        private Dictionary<FSMTriggerID, FSMStateID> map;
        public List<FSMTrigger> Triggers;
        public void AddMap(FSMTriggerID triggerID,FSMStateID stateID)
        {
            map.Add(triggerID, stateID);
            CreateTrigger(triggerID);
        }
        public FSMState()
        {
            Init();
            map=new Dictionary<FSMTriggerID, FSMStateID>();
            Triggers=new List<FSMTrigger>();
        }

        public abstract void Init();
        private void CreateTrigger(FSMTriggerID triggerID)
        {
            //命名规范：（命名空间.）+条件枚举 + Trigger
            Type type = Type.GetType("ns."+triggerID + "Trigger");
            FSMTrigger trigger = Activator.CreateInstance(type) as FSMTrigger;
            Triggers.Add(trigger);
        }
        public void Reason(FSMBase fsm)
        {
            for (int i = 0; i < Triggers.Count; i++)
            {
                if (Triggers[i].HandleTrigger(fsm))
                {
                    FSMStateID stateID = map[Triggers[i].TriggerID];
                    fsm.ChangeActiveState(stateID);
                    return;
                }
            }
        }
        public virtual void EnterState(FSMBase fsm)
        {

        }
        public virtual void ActionState(FSMBase fsm)
        {

        }
        public virtual void ExitState(FSMBase fsm)
        {

        }
    }
}